package com.kk.quyan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kk.quyan.common.R;
import com.kk.quyan.entity.Material;
import com.kk.quyan.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;

/**
 * @description:
 * @author: Kk
 * @create: 2020-11-21 14:58
 **/
@Api(value = "MaterialController" ,description = "素材测试接口")
@RestController
@RequestMapping("material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @ApiOperation(value = "分页查询不同的标签下素材列表，并按照更新时间倒序")
    @GetMapping("getMaterialPage/{labelId}/{page}/{limit}")
    public R getMaterialPage(@PathVariable("labelId")int labelId,
                             @PathVariable("page")int page,@PathVariable("limit")int limit){
        if (page<=1){
            page=1;
        }
        List<Material> materials=materialService.getMaterials(labelId,page,limit);
        if (materials==null){
            return R.ok().setMessage("该标签无内容");
        }else {
            return R.ok().data("materials",materials);
        }
    }

    @ApiOperation("统计不同标签下素材的个数")
    @GetMapping("countMaterialByLabel")
    public R countMaterialByLabel(int labelId){
        int count = materialService.countMaterial(labelId);
        return R.ok().data("count",count);
    }

    @ApiOperation(value = "插入单个素材")
    @PostMapping("insertMaterial")
    public R insertMaterial(@RequestBody Material material){
        boolean flag = materialService.save(material);
        return flag?R.ok():R.error();
    }

    @ApiOperation(value = "删除单个素材")
    @PostMapping("deleteMaterial")
    public R deleteMaterial(Integer mId){
        boolean flag = materialService.removeById(mId);
        return flag?R.ok():R.error();
    }

    @ApiOperation(value = "批量插入多个素材")
    @PostMapping("bulkInsertMaterials")
    public R bulkInsert(@RequestBody List<Material> materials){
        boolean flag = materialService.saveBatch(materials);
        return flag?R.ok():R.error();
    }

    @ApiOperation(value = "批量插入多个素材")
    @PostMapping("bulkDeleteMaterials")
    public R bulkDelete(@RequestBody List<Integer> mIds){
        boolean flag = materialService.removeByIds(mIds);
        return flag?R.ok():R.error();
    }

    @ApiOperation(value = "Excel批量插入多个素材,excel文件头格式 title type url description")
    @PostMapping("bulkInsertWithExcel")
    public R bulkInsertWithExcel(MultipartFile file){
        materialService.saveExcel(file,materialService);
        return R.ok();
    }

    @ApiOperation(value = "Excel导出素材列表")
    @GetMapping("exportMaterialsExcel")
    public R exportMaterialsExcel(@RequestParam(required = false)Integer type){
        if (type==null){
            materialService.exportExcel(-1);
        }else {
            materialService.exportExcel(type);
        }
        return R.ok();
    }

    @ApiOperation(value = "模糊查询素材")
    @GetMapping("search/{keyword}/{page}/{limit}")
    public R search(@PathVariable("keyword")String keyword,@PathVariable("page")int page,@PathVariable("limit")int limit){
        IPage<Material> results = materialService.getMaterialsByKeyword(keyword, page, limit);
        //分类
        List<Material> list = results.getRecords();
        Map<Integer, List<Material>> map=list.stream().collect(groupingBy(Material::getType));
        return R.ok().data("data",map)
                     .data("total",results.getTotal());
    }

}
